/***************************************************************
Module name: LibSpyDll.cpp
Copyright (c) 2003 Robert Kuster

Notice:	If this code works, it was written by Robert Kuster.
		Else, I don't know who wrote it.

		Use it on your own risk. No responsibilities for
		possible damages of even functionality can be taken.
***************************************************************/

#include <windows.h>
#include "stdio.h"
#include "LibSpyDll.h"

//-------------------------------------------------------
// shared data 
// Notice:	seen by both: the instance of "LibSpy.dll" mapped
//			into the remote process as well as by the instance
//			mapped into our "LibSpy.exe"
#pragma data_seg (".shared")
bool			g_Initialized = false;
double			g_UncommittedSize = 0;
char			g_szProcessHeapPtr [128] = { '\0' };
unsigned long	g_VMUsageMB = 0;
bool			g_Break = false;
#pragma data_seg ()

#pragma comment(linker,"/SECTION:.shared,RWS") 

typedef  bool (*LPFNDLLFUNC1)(unsigned long *megabytesRemaining, double *percentFull);


BOOL APIENTRY DllMain( HANDLE hModule, 
                       DWORD  ul_reason_for_call, 
                       LPVOID lpReserved
					 )
{	
	if( (ul_reason_for_call == DLL_PROCESS_ATTACH) && g_Initialized ) 
	{
		MEMORYSTATUSEX statex;
		statex.dwLength = sizeof (statex);
		GlobalMemoryStatusEx (&statex);

		g_UncommittedSize = statex.ullAvailVirtual;

		PROCESS_HEAP_ENTRY heapEntry;
		memset(&heapEntry,0,sizeof(heapEntry));

		while(HeapWalk(GetProcessHeap(), &heapEntry))
			g_UncommittedSize += heapEntry.cbData;			

	}
	return TRUE;
}

